# -*- coding: utf-8
from ConfigParser import SafeConfigParser

from PyQt4 import QtGui, QtCore
from QDirectoryChooser import QDirectoryChooser

class AttributeDict(dict):
	
	def __getattribute__(self, name):
		try:
			return super(AttributeDict, self).__getattribute__(name)
		except Exception, e:
			try:
				return self[name]
			except KeyError, k:
				return ""

	def __setattr__(self, name, value):
		self[name] = str(value)

class Config(object):
	
	file_name = "confi.ini"
	
	def __init__(self):
		self.c = SafeConfigParser()
		self.c.read(Config.file_name)
		self._dic = {}
		self.load_dict()

	def __getitem__(self, item):
		if not self._dic.has_key(item):
			self._dic[item] = AttributeDict()
		
		return self._dic[item]
	def load_dict(self):
		for section in self.c.sections():
			self._dic[section] = AttributeDict()
			
			for option, value in self.c.items(section):
				self._dic[section][option] = value
		
	def load_config(self):
		for section_name, section in self._dic.items():
			if not self.c.has_section(section_name):
				self.c.add_section(section_name)
			
			for option, value in section.items():
				self.c.set(section_name, option , value)
	
	def write(self):
		self.load_config()
		file = open(Config.file_name, "w")
		self.c.write(file)

class QConfiguration(QtGui.QDialog):
	
	def __init__(self, parent):
		super(QConfiguration, self).__init__(parent)
		
		self.config = Config()
		
		self.temporaryPath = QDirectoryChooser(self, title=self.tr("Escolha o path para os arquivos temporarios"))
		self.temporaryPath.edit.setText(self.config["general"].temporary_path)
		
		value = int(self.config["general"].slices) if self.config["general"].slices else 0
		self.slices = QtGui.QSpinBox(self)
		self.slices.setMaximum(10)
		self.slices.setMaximumSize(60, 20)
		self.slices.setValue(value)
		
		self.iconsPath = QDirectoryChooser(self, title=self.tr("Escolha o path para is ícones: "))
		self.iconsPath.edit.setText(self.config["general"].icons_path)
		
		self.btnCancel = QtGui.QPushButton(self.tr("Cancelar"))
		self.btnOk = QtGui.QPushButton(self.tr("Ok"))
		self.btnOk.setDefault(True)
		
		self.bottomLayout = QtGui.QHBoxLayout()
		self.bottomLayout.addStretch(1)
		self.bottomLayout.addWidget(self.btnOk)
		self.bottomLayout.addWidget(self.btnCancel)
		
		self.configLayout = QtGui.QGridLayout()
		self.configLayout.addWidget(QtGui.QLabel(self.tr("Path para arquivos temporarios: ")), 0, 0)
		self.configLayout.addWidget(self.temporaryPath, 0, 1)
		self.configLayout.addWidget(QtGui.QLabel(self.tr("Número de fatias por download: ")), 1, 0)
		self.configLayout.addWidget(self.slices, 1, 1)
		self.configLayout.addWidget(QtGui.QLabel(self.tr("Path para os ícones: ")), 2, 0)
		self.configLayout.addWidget(self.iconsPath, 2, 1)
		
		self.mainLayout = QtGui.QVBoxLayout()
		self.mainLayout.addLayout(self.configLayout)
		self.mainLayout.addLayout(self.bottomLayout)
		
		self.connect(self.btnOk, QtCore.SIGNAL("clicked()"), self.on_btnOk_clicked)
		self.connect(self.btnCancel, QtCore.SIGNAL("clicked()"), self.on_btnCancel_clicked)
		
		self.setLayout(self.mainLayout)
	
	def on_btnOk_clicked(self):
		self.config["general"].temporary_path = self.temporaryPath.edit.text()
		self.config["general"].slices = self.slices.value()
		self.config["general"].icons_path = self.iconsPath.edit.text()
		self.config.write()
		self.close()
	
	def on_btnCancel_clicked(self):
		self.close()

if __name__ == "__main__":
	import sys
	app = QtGui.QApplication(sys.argv)
	config = QConfiguration(None)
	sys.exit(config.exec_())